<template>
  <a-modal
    title="新建学生"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="学生学号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['studentCode', {rules: [
                  { required: true, message: '请输入学生学号', trigger: 'blur' }]}]"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="学生姓名"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['studentName', {rules: [{required: true, message: '请输入学生姓名', trigger: 'blur'}]}]"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="学生角色"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select
                v-decorator="['roleId', {initialValue: '3', rules: [{required: true, message: '请选择角色'}]}]"
                placeholder="请选择"
                disabled
              >
                <a-select-option v-for="role in roles" :key="role.id">{{ role.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { addStudent, validateStudentCode } from '@/api/basic/student'

export default {
  props: {
    roles: {
      type: Array,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    add () {
      this.visible = true
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          validateStudentCode({ 'studentCode': values.studentCode })
            .then(res => {
              if (res.message !== 'error') {
                addStudent(values)
                  .then(res => {
                    if (res.message !== 'error') {
                      this.visible = false
                      this.confirmLoading = false
                      this.form.resetFields()
                      this.$emit('ok')
                    } else {
                      this.confirmLoading = false
                      this.$message.error('操作失败！')
                    }
                  })
              } else {
                this.confirmLoading = false
                this.$message.error('学生学号已经存在！')
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
